using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// Author:SQB
    /// </summary>
    public partial class WorkSubCategoryEntry : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140806";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
            }
            
        }

        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
        }

        private void SetPageLoad()
        {
            if (this.Request["AdmRegSubCategoryID"] != null)
            {
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.GetWorkSubCategoryByID(this.LangCode, int.Parse(this.Request["AdmRegSubCategoryID"].ToString()));
     
                this.hidID.Value = dt.Rows[0]["AdmRegSubCategoryID"].ToString();
                this.txtSubCatEN.Text = dt.Rows[0]["Description1"].ToString();
                this.txtSubCatCH.Text = dt.Rows[0]["Description2"].ToString();
                this.txtSubCatHK.Text = dt.Rows[0]["Description3"].ToString();
                //Add By Gordon 20150702 U18
                this.txtSequence.Text = dt.Rows[0]["Sequence"].ToString();

                this.chkSignReq.Checked = bool.Parse(dt.Rows[0]["FlagReqSign"].ToString());
                this.chkProtect.Checked = bool.Parse(dt.Rows[0]["FlagProtect"].ToString());
                this.chkVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());
                this.ddlWorkCategory.SelectedValue = dt.Rows[0]["AdmRegCategoryID"].ToString();
                this.ddlWorkCategory.Enabled = false;

                if (this.chkVoid.Checked == true)
                {
                    this.ddlWorkCategory.Enabled = false;
                    this.txtSubCatCH.Enabled = false;
                    this.txtSubCatEN.Enabled = false;
                    this.txtSubCatHK.Enabled = false;
                    //Add By Gordon 20150702 U18
                    this.txtSequence.Enabled = false;

                    this.chkVoid.Enabled = false;
                    this.chkSignReq.Enabled = false;
                    this.chkProtect.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnSaveAndNew.Enabled = false;
                    this.btnSaveAndReturn.Enabled = false;
                }
       
                dt.Dispose();
                dt = null;
            }

            //Update By Gordon 20150702 U18
            //this.btnSave.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            //this.btnSaveAndNew.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            //this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");

            this.btnSave.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_SeqMandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndNew.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_SeqMandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_Desc1Mandatory") + "','" + this.GetMessage("SOM_SeqMandatory") + "','" + this.GetMessage("SOM_VoidSure") + "')");
            //Update End
        }

        /// <summary>
        /// Bind drop down list Work Category
        /// </summary>
        private void BindWorkCategory()
        {
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegCategory.GetWorkCategory(this.LangCode,"",false);

            this.ddlWorkCategory.Items.Clear();
            ddlWorkCategory.DataSource = dt;
            ddlWorkCategory.DataValueField = "AdmRegCategoryID";
            ddlWorkCategory.DataTextField = "Description";
            ddlWorkCategory.DataBind();
            //ddlWorkCategory.Items.Insert(0, LiBlank());
            ddlWorkCategory.SelectedIndex = 0;

            dt.Dispose();
            dt = null;
        }

        private void SetLanguage()
        {
            this.lbl_CodeSetupTitle.Text = this.GetLabel("SOM_CodeSetupWorkSubCategoryEntry");
            this.lblWorkCategory.Text = this.GetLabel("SOM_WorkCategory",true);
            this.lblWorkSubCategory.Text = this.GetLabel("SOM_WorkSubCategory");
            this.lblSubCatEN.Text = this.GetLabel("SOM_WorkSubCategoryDesc1", true);
            this.lblSubCatCH.Text = this.GetLabel("SOM_WorkSubCategoryDesc2", true);
            this.lblSubCatHK.Text = this.GetLabel("SOM_WorkSubCategoryDesc3", true);
            //Add By Gordon 20150702 U18
            this.lblSequence.Text = this.GetLabel("SOM_WCSequence", true);

            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnSaveAndReturn.Text = this.GetLabel("COM_SaveAndReturn");
            this.btnSaveAndNew.Text = this.GetLabel("SOM_SaveNew");
            this.btnCancel.Text = this.GetLabel("COM_Cancel");

            this.chkSignReq.Text = this.GetLabel("SOM_SignReq", true);
            this.chkProtect.Text = this.GetLabel("SOM_Proection", true);
            this.chkVoid.Text = this.GetLabel("SOM_Void", true);

            BindWorkCategory();
        }

        /// <summary>
        /// CheckSeqUnique
        /// Add By Gordon 20150702 U18
        /// </summary>
        /// <returns></returns>
        private bool CheckSeqUnique()
        {
            return BLL.SOM.CodeSetup.CDAdmRegSubCategoryBLL.CheckUniqueSeq(int.Parse(txtSequence.Text), 2,int.Parse(ddlWorkCategory.SelectedValue));
        }

        private int Save(int admRegSubCategoryID, int action)
        {
            return MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.Update(
                                                                        admRegSubCategoryID,
                                                                        int.Parse(this.ddlWorkCategory.SelectedValue),
                                                                        this.txtSubCatEN.Text.Trim(),
                                                                        this.txtSubCatCH.Text.Trim(),
                                                                        this.txtSubCatHK.Text.Trim(),
                                                                        this.txtSequence.Text.Trim(),//Add By Gordon 20150702
                                                                        this.chkSignReq.Checked,
                                                                        this.chkProtect.Checked,
                                                                        this.ODMUser.ID,
                                                                        DateTime.Now,
                                                                        this.chkVoid.Checked,
                                                                        this.ODMUser.ID,
                                                                        DateTime.Now,
                                                                        this.ODMUser.ID,
                                                                        action
                                                                        );
        }


        private void Clear()
        {
            this.txtSubCatCH.Text = "";
            this.txtSubCatEN.Text = "";
            this.txtSubCatHK.Text = "";
            //add By gordon 20150702 U18
            this.txtSequence.Text = "";

            this.ddlWorkCategory.Enabled = true;
            this.ddlWorkCategory.SelectedIndex = 0;
            this.chkVoid.Checked = false;
            this.chkSignReq.Checked = false;
            this.chkProtect.Checked = false;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int id;

            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    //Add By Gordon 20150702 U18
                    if (CheckSeqUnique())
                    {
                        this.Alert(this.GetMessage("SOM_SubSeqUnique"));
                        return;
                    }

                    id = Save(0, 1);
                    this.hidID.Value = id.ToString();
                    this.ddlWorkCategory.Enabled = false;
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    if (this.chkVoid.Checked == true)
                    {
                        AfterVoid();
                    }
                }
            }
            else
            {
                if (CheckUnique(false))
                {
                    id = Save(int.Parse(this.hidID.Value), 2);
                    this.ddlWorkCategory.Enabled = false;
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    if (this.chkVoid.Checked == true)
                    {
                        AfterVoid();
                    }
                }
            }
            
            
        }

        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            int id;

            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    //Add By Gordon 20150702 U18
                    if (CheckSeqUnique())
                    {
                        this.Alert(this.GetMessage("SOM_SubSeqUnique"));
                        return;
                    }

                    id = Save(0, 1);
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    string script = "<script language=javascript>function btnChange_Click(){window.location.href='WorkSubCategoryList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                    this.Page.RegisterStartupScript("aaa", script);
                }
                else
                {
                    return;
                }
            }
            else
            {
                id = Save(int.Parse(this.hidID.Value), 2);
                this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                string script = "<script language=javascript>function btnChange_Click(){window.location.href='WorkSubCategoryList.aspx'} window.setTimeout(btnChange_Click,150);" + "</script>";
                this.Page.RegisterStartupScript("aaa", script);
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int id;
            if (this.hidID.Value == String.Empty)
            {
                if (CheckUnique(true))
                {
                    //Add By Gordon 20150702 U18
                    if (CheckSeqUnique())
                    {
                        this.Alert(this.GetMessage("SOM_SubSeqUnique"));
                        return;
                    }

                    id = Save(0, 1);
                    this.hidID.Value = "";
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    Clear();
                }
            }
            else
            {
                if (CheckUnique(false))
                {
                    id = Save(int.Parse(this.hidID.Value), 2);
                    this.hidID.Value = "";
                    this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
                    Clear();
                }
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("WorkSubCategoryList.aspx");
        }

        private bool CheckUnique(bool insert)
        {
            if (insert == true)
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.CheckWorkSubCategoryUnqiue(this.txtSubCatEN.Text.Trim(), 1,-1).Rows.Count > 0 && this.txtSubCatEN.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.CheckWorkSubCategoryUnqiue(this.txtSubCatCH.Text.Trim(), 2, -1).Rows.Count > 0 && this.txtSubCatCH.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.CheckWorkSubCategoryUnqiue(this.txtSubCatHK.Text.Trim(), 3, -1).Rows.Count > 0 && this.txtSubCatHK.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc3Unique"));
                    return false;
                }
                return true;
            }
            else
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.CheckWorkSubCategoryUnqiue(this.txtSubCatEN.Text.Trim(), 1,int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtSubCatEN.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc1Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.CheckWorkSubCategoryUnqiue(this.txtSubCatCH.Text.Trim(), 2, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtSubCatCH.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc2Unique"));
                    return false;
                }
                else if (MTRC.ODMS.DAL.SOM.CodeSetup.CDAdmRegSubCategory.CheckWorkSubCategoryUnqiue(this.txtSubCatHK.Text.Trim(), 3, int.Parse(this.hidID.Value)).Rows.Count > 0 && this.txtSubCatHK.Text.Trim() != String.Empty)
                {
                    this.Alert(this.GetMessage("SOM_Desc3Unique"));
                    return false;
                }
                return true;
            }
        }


        private void AfterVoid()
        {
            this.txtSubCatCH.Enabled = false;
            this.txtSubCatEN.Enabled = false;
            this.txtSubCatHK.Enabled = false;
            //Add By Gordon 20150702 U18
            this.txtSequence.Enabled = false;
            this.chkVoid.Enabled = false;
            this.chkSignReq.Enabled = false;
            this.chkProtect.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnSaveAndNew.Enabled = false;
            this.btnSaveAndReturn.Enabled = false;
        }
    }
}
